package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.heaven.entity.ResourceType;
import org.apache.commons.collections.map.AbstractReferenceMap;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * 资源类型Dao
 *
 * @author fanruiping
 * @since 2014-12-05
 */
@Repository
public class ResourceTypeDao extends BaseDao {
    /**
     * 获取所有资源类型
     * @return List
     *
     * @author fanruiping
     * @since 2014-12-05
     */
    public List<ResourceType> getAll(){
        String sql = "SELECT * FROM t_resource_type";

        log.debug("获取所有资源类型集合");

        return namedParameterJdbcTemplate.query(sql, new MapSqlParameterSource(), CreateMapper());
    }

    /**
     * 根据id获取资源明细
     * @param id 编号
     * @return List
     *
     * @author fanruiping
     * @since 2014-12-22
     */
    public ResourceType getById(Long id){
        try {
            String sql = "SELECT * FROM t_resource_type WHERE id=:id";

            log.debug("return resource type by id");

            return namedParameterJdbcTemplate.queryForObject(sql, new MapSqlParameterSource().addValue("id", id), CreateMapper());
        }catch (EmptyResultDataAccessException ex){
            log.debug("catch EmptyResultDataAccessException:return null");

            return null;
        }
    }

    /**
     * 根据id获取资源明细
     * @param ids 编号集合
     * @return List
     *
     * @author fanruiping
     * @since 2015-05-07
     */
    public Map<Long,String> getNameByIds(Set<Long> ids){
        final Map<Long,String> result = new HashMap<>();

            String sql = "SELECT id,sName FROM t_resource_type WHERE id IN(:id)";

            namedParameterJdbcTemplate.query(sql, new MapSqlParameterSource().addValue("id", ids), new RowMapper<Object>() {
                @Override
                public Object mapRow(ResultSet rs, int i) throws SQLException {
                    result.put(rs.getLong("id"), rs.getString("sName"));

                    return null;
                }
            });

        return result;
    }

    /**
     * 查询指定资源类型的ID
     * 
     * @param typeName 类型名称
     * @return Long
     * 
     * @author cx
     * @since 2015/4/6
     */
    public Long getIdByName(final String typeName) {
        String sql = "SELECT id FROM t_resource_type WHERE sName=:typeName";
        
        try {
            log.debug("查询指定资源类型的ID");
            return namedParameterJdbcTemplate.queryForObject(sql, new MapSqlParameterSource().addValue("typeName", typeName), Long.class);
        } catch (EmptyResultDataAccessException ex) {
            log.debug("获取资源ID为null");
            return null;
        }
    }

    /**
     * ResultSet转为ResourceType实体
     * @return RowMapper
     *
     * @author fanruiping
     * @since 2014-12-05
     */
    private RowMapper<ResourceType> CreateMapper(){
        return new RowMapper<ResourceType>() {
            @Override
            public ResourceType mapRow(ResultSet rs, int i) throws SQLException {
                ResourceType entity = new ResourceType();

                entity.setId(rs.getLong("id"));
                entity.setName(rs.getString("sName"));

                return entity;
            }
        };
    }
}
